//
//  CZComposeToolBar.swift
//  weiBo
//
//  Created by apple on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum CZComposeToolBarButtonType:Int {

    //图片
    case picture = 0
    case Mention = 1
    //话题
    case Trend = 2
    //表情
    case Emoticon = 3
    //add
    case Add = 4
}


@available(iOS 9.0, *)

class CZComposeToolBar: UIStackView {
    
    //建立回传闭包
    
    var didSeletedButtonClosure: ((type: CZComposeToolBarButtonType)->())?
    
    //懒加载按钮
    private lazy var emotionBtn: UIButton = {
        let btn = UIButton()
        
        return btn
    }()


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        axis = .Horizontal
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture",type: .picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emotionBtn = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        addChildButton("compose_add_background",type: .Add)

    
    }
    
    //添加子空间的方法
    private func addChildButton(imageName: String,type: CZComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        //  设置背景图片 
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //  去掉高亮效果
        button.adjustsImageWhenHighlighted = false
    
        addArrangedSubview(button)
        
        return button
    }
    
      @objc private func buttonClick(btn: UIButton) {
        print("点击了按钮")
        //通过tag值取得枚举值
        let type = CZComposeToolBarButtonType(rawValue: btn.tag)!
        didSeletedButtonClosure?(type: type)
    }
    
    //选择表情键盘的放法
    func showEmoticonIcon(isEmotion: Bool) {
        var icon = "compose_emoticonbutton_background"

        if isEmotion {
            icon = "compose_keyboardbutton_background"
        }
        emotionBtn.setImage(UIImage(named: icon), forState: .Normal)
        emotionBtn.setImage(UIImage(named: "\(icon)_highlighted"), forState: .Highlighted)
    
    
    }
    
    
}
